package xyz.yohao.flow.admin.controller.flow;

import cn.hutool.core.util.ObjectUtil;
import lombok.val;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.yohao.flow.bean.entity.BaseEntity;
import xyz.yohao.flow.common.constants.EntityConstants;
import xyz.yohao.flow.security.util.SecurityUtils;

import java.util.Date;

public class BaseFlowController {

    @Autowired
    protected MapperFacade mapperFacade;

    /**
     * 设置基础信息
     *
     * @param entity
     */
    protected void setBaseEntity(BaseEntity entity) {

        val sysUser = SecurityUtils.getSysUser();

        if (ObjectUtil.isNull(entity.getCreateTime())) {
            entity.setCreateTime(new Date());
            entity.setCreateBy(sysUser.getUsername());
        }
        entity.setUpdateTime(new Date());
        entity.setUpdateBy(sysUser.getUsername());

        if (ObjectUtil.isNull(entity.getIsDeleted())) {
            entity.setIsDeleted(EntityConstants.ENTITY_NOT_DEL_FLAT);
        }

        if (ObjectUtil.isNull(entity.getTenantId())) {
            entity.setTenantId(sysUser.getShopId());
        }
    }
}
